package io.chenglicun.modules.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.BaseServiceImpl;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.modules.bus.dao.BusAdressBookDao;
import io.chenglicun.modules.bus.dto.BusAdressBookDTO;
import io.chenglicun.modules.bus.entity.BusAdressBookEntity;
import io.chenglicun.modules.bus.service.BusAdressBookService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 业务_通讯录
 *
 * @author LYB jssqsylyb@163.com
 * @since 1.0 2023-09-04
 */
@Service
public class BusAdressBookServiceImpl extends BaseServiceImpl<BusAdressBookDao, BusAdressBookEntity> implements BusAdressBookService {

    public QueryWrapper<BusAdressBookEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<BusAdressBookEntity> wrapper = new QueryWrapper<>();

        String name = (String)params.get("name");
        wrapper.like(StringUtils.isNotBlank(name), "name", name);
        String gender = (String)params.get("gender");
        wrapper.eq(StringUtils.isNotBlank(gender), "gender", gender);
        String deptName = (String)params.get("deptName");
        wrapper.like(StringUtils.isNotBlank(deptName), "dept_name", deptName);
        String jobName = (String)params.get("jobName");
        wrapper.like(StringUtils.isNotBlank(jobName), "job_name", jobName);
        String mobile = (String)params.get("mobile");
        wrapper.like(StringUtils.isNotBlank(mobile), "mobile", mobile);
        wrapper.orderByDesc("create_date","sort");
        return wrapper;
    }


    @Override
    public PageData<BusAdressBookDTO> page(Map<String, Object> params) {
        IPage<BusAdressBookEntity> page = baseDao.selectPage(
                getPage(params, "sort", true),
                getWrapper(params)
        );

        return getPageData(page, BusAdressBookDTO.class);
    }

    @Override
    public BusAdressBookDTO get(Long id) {
        BusAdressBookEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, BusAdressBookDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(BusAdressBookDTO dto) {
        BusAdressBookEntity entity = ConvertUtils.sourceToTarget(dto, BusAdressBookEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BusAdressBookDTO dto) {
        BusAdressBookEntity entity = ConvertUtils.sourceToTarget(dto, BusAdressBookEntity.class);

        updateById(entity);
    }

    @Override
    public void delete(Long[] ids) {
        deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<BusAdressBookDTO> list(Map<String, Object> params) {
        List<BusAdressBookEntity> entityList = baseDao.selectList(getWrapper(params).orderByAsc("sort"));

        return ConvertUtils.sourceToTarget(entityList, BusAdressBookDTO.class);
    }

    @Override
    public Integer getMaxSort() {
        return baseDao.getMaxSort()+1;
    }
}
